package com.share.example.selenium.service.hzhc.dingding;

import com.share.common.utils.DateUtil;
import com.share.common.utils.DelayUtil;
import com.share.example.selenium.service.AbsDriver;
import com.share.example.selenium.utils.DriverUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Data
@Slf4j
@Service
public class HcxxDingDingService extends AbsDriver {
    private final String LOGIN_URL_PRE = "https://v4.21tb.com/mix-pc/newHome";
    private final String LOGIN_SUCCESS_FORWORD_URL_PRE = "https://sellercentral.amazon.com/ap/signin";

    @Autowired
    protected WebDriver driver;

    @Override
    public void reInitDriver(WebDriver driver) {
        this.driver = driver;
    }

    //1.打开卖家---填写表单页面
    public Boolean openLoginPage() {
        //1.打开首页
        String url = "https://v4.21tb.com/wx/auth3rd/auth/DINGTALK/ihanchen.do?mobileUrl=L21peC1oNS8jL3NoYXJlU2NhbkVudGVyP3NjZW5lPUVWQnpVal9ERVRBSUwmdHlwZT1oNV9uZXcmaWQ9MWQ1NWVkN2YxMzJhNDk0MDg1OTAxYjZiYTZkMmQ1OTcmcHJvamVjdFR5cGU9TUlYX1BMQU4-&pcUrl=L21peC1wYy9ob21lP3Byb2plY3RJZD0xZDU1ZWQ3ZjEzMmE0OTQwODU5MDFiNmJhNmQyZDU5NyZwcm9qZWN0VHlwZT1NSVhfUExBTiZwcm9qZWN0TmFtZT3nur@kuIrlrabkuaDnsr7lh4bmjqjpgIHnrKzkuozljYHlm5vmnJ@vvIjnoJTlj5EtSkFWQe_8iQ--";
        driver.get(url);

        //访问会重定向
        String currentUrl = driver.getCurrentUrl();

        //2.钉钉扫码//等待扫码
        while (currentUrl.equals(driver.getCurrentUrl())) {
            log.info("等待钉钉扫码");
            DelayUtil.sleepSecond(2);
        }
        log.info("用户已经扫码完毕");
        DelayUtil.sleepSecond(5);
        currentUrl = driver.getCurrentUrl();
        if (currentUrl.contains(LOGIN_URL_PRE)) {
            return true;
        } else {
            log.info("打开登录表单页面失败,currentUrl={}", currentUrl);
            return false;
        }
    }


    //打开卖家表单页面
    public Boolean taskAll() {
        //1.点击https://v4.21tb.com/mix-pc/newHome 里面的全部
        log.info("点击-全部");
        WebElement ele_all = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[1]/span[1]"));
        ele_all.click();
        DelayUtil.sleepSecond(3);
        String windowHandle1 = driver.getWindowHandle();

        log.info("点击-全部下面的第一个任务");
        WebElement ele_first_task = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div[1]/div[2]/img"));
        ele_first_task.click();
        DelayUtil.sleepSecond(5);

        DriverUtil.switchToTarget(driver, null, windowHandle1);
        String windowHandle2 = driver.getWindowHandle();

        log.info("点击task1的任务详情");
        WebElement ele_task1_home = driver.findElement(By.xpath("//*[@id=\"891c5733f1c348e19ce3a2d56e308b55\"]/div[1]/div[2]/div/div[1]"));
        ele_task1_home.click();
        DelayUtil.sleepSecond(3);
        DriverUtil.switchToTarget(driver, null, windowHandle1, windowHandle2);
        String windowHandle3 = driver.getWindowHandle();


        //点击学习
        log.info("点击学习");
        WebElement ele_xuexi = driver.findElement(By.linkText("进入学习"));
        ele_xuexi.click();
        DelayUtil.sleepSecond(8);

        return true;
    }

    /**
     * JavascriptExecutor jse = (JavascriptExecutor) driver;
     * jse.executeScript("return arguments[0].currentSrc", video);//获得视频的URL
     * jse.executeScript("return arguments[0].play()", video);//播放视频，播放15秒
     * jse.executeScript("arguments[0].pause()", video);  // 暂停视频
     *
     * @return
     */
    public Integer bofang() {
        //提取页面列表
        int i = 0;
        Integer bofangCount = 0;
        log.info("提取页面的播放列表");
        WebElement courseItemId = driver.findElement(By.id("courseItemId"));
        List<WebElement> list = courseItemId.findElements(By.tagName("li"));
        log.info("待播放视频数={}", list.size());
        String windowHandle = driver.getWindowHandle();
        for (WebElement li : list) {
            try {
                i++;
                li.click();
                DelayUtil.sleepSecond(8);
                log.info("开始播放-第{}个视频,date={}", i, DateUtil.getDateTime());

                // 定位视频播放控件
                WebDriver iframe = driver.switchTo().frame(driver.findElement(By.id("iframe_aliplayer")));
                WebElement video = iframe.findElement(By.id("duoscreen")).findElement(By.tagName("video"));

                long start = System.currentTimeMillis();
                long end = System.currentTimeMillis();
                do {
                    DriverUtil.videoPlay(iframe, video);//播放
                    DelayUtil.sleepSecond(60 * 5);
                    DriverUtil.videoPause(iframe, video);  // 暂停
                    DelayUtil.sleepSecond(3);
                    end = System.currentTimeMillis();
                } while (((end - start) / 1000) < (60 * 30));
                log.info("第{}个视频播放完毕,date={}", i, DateUtil.getDateTime());
                bofangCount++;
            } catch (Exception e) {
                e.printStackTrace();
                bofangCount = null;
            }
        }
        return bofangCount;
    }
}
